<?php

namespace Yearnfar\Bundle\CmsBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * @Route("/file")
 * @Template()
 */
class FileController extends BaseController
{

    /**
     * @Route("/upload/{name}")
     * @param $name
     * @return array
     */
    public function uploadAction($name)
    {
        $files = $this->get('request')->files->all();
        if ($files) {
            $relDir = '/upload/' . $name . '/' . date('Ym', time()) . '/' . date('md', time());
            //
            foreach ($files as $file) {
                $dstDir = $this->get('kernel')->getRootDir() . "/../web/{$relDir}";
                //
                $file->move($dstDir, $file->getClientOriginalName());
                $data['link'][] = $relDir . '/' . $file->getClientOriginalName();
            }
            //kindEditor特殊处理
            if ($name=='kindEditor') {
                $data = ['error'=>0, 'url'=>$data['link'][0]];
                return $this->jsonOut($data, false);
            }
            return $this->jsonOut($data);
        }
        return [];
    }

}
